package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
	
	@Autowired
	private IEmployeeService employeeService;

	@Autowired
	private IRoleService roleService;


	@Autowired
	private IDepartmentService departmentService;


	@RequestMapping("list")
	public String list(Model model,@ModelAttribute("qo")EmployeeQueryObject qo){
		model.addAttribute("result", employeeService.query(qo));
		model.addAttribute("depts", departmentService.selectAll());
		return "/employee/list";
	}
	@RequestMapping("/input")
	public String input(Long id,Model model){
		if(id != null){
			model.addAttribute("entity",employeeService.selectByPrimaryKey(id));
		}
		model.addAttribute("depts", departmentService.selectAll());
		model.addAttribute("roles", roleService.selectAll());
		return "/employee/input";
	}


	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Employee entity,Long[] ids){
		if(entity.getId() != null){
			employeeService.updateByPrimaryKey(entity,ids);
		}else{
			employeeService.insert(entity,ids);
		}

		return "redirect:/employee/list.do";
	}


	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id){
		JSONResult result = new JSONResult();
		try {
			if(id != null){
                employeeService.deleteByPrimaryKey(id);
            }
		} catch (Exception e) {
			e.printStackTrace();
			result.mark(e.getMessage());
		}
		return result;
	}

	@RequestMapping("/batchDelete")
	@ResponseBody
	public Object batchDelete(Long[] ids){
		JSONResult result = new JSONResult();
		try {
			if(ids != null && ids.length >0){
				employeeService.batchDelete(ids);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.mark(e.getMessage());
		}

		return result;
	}






	@RequestMapping("exportXls")
	public void exportXls(HttpServletResponse response) throws IOException {
		response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("day01");
		HSSFRow row = sheet.createRow(0);
		row.createCell(0).setCellValue("姓名");
		row.createCell(1).setCellValue("邮箱");
		row.createCell(2).setCellValue("年龄");
		List<Employee> employees = employeeService.selectAll();
		for (int i = 1; i <=employees.size() ; i++) {
			Employee employee = employees.get(i - 1);
			row = sheet.createRow(i);
			row.createCell(0).setCellValue(employee.getName());
			row.createCell(1).setCellValue(employee.getEmail());
			row.createCell(2).setCellValue(employee.getAge());
		}
		wb.write(response.getOutputStream());

	}

	@RequestMapping("importData")
	public String importData(MultipartFile file) throws IOException {
		HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
		HSSFSheet sheet = wb.getSheet("day01");
		int lastRowNum = sheet.getLastRowNum();
		for (int i = 1; i <=lastRowNum ; i++) {
			Employee employee = new Employee();
			HSSFRow row = sheet.getRow(i);
			employee.setName(row.getCell(0).getStringCellValue());
			employee.setEmail(row.getCell(1).getStringCellValue());
			employee.setAge(Integer.parseInt(row.getCell(2).getStringCellValue()));
			employee.setPassword("1");
			if(employee != null){
				employeeService.insert(employee,null);
			}

		}
		return "redirect:/employee/list.do";

	}



}



	/*@RequiredPermission("員工列表")
	@RequestMapping("list")
	public String list(Model model,@ModelAttribute("qo")EmployeeQueryObject qo){
		model.addAttribute("result", employeeService.query(qo));
		model.addAttribute("depts", departmentService.selectAll());
		return "/employee/list";
	}
	@RequiredPermission("員工編輯")
	@RequestMapping("/input")
	public String input(Long id,Model model){
		if(id != null){
			model.addAttribute("entity",employeeService.selectByPrimaryKey(id));
		}
		model.addAttribute("depts", departmentService.selectAll());
		model.addAttribute("roles", roleService.selectAll());
		return "/employee/input";
	}
	@RequiredPermission("員工更新/保存")
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Employee entity,Long[] ids){
		if(entity.getId() != null){
			employeeService.updateByPrimaryKey(entity,ids);
		}else{
			employeeService.insert(entity,ids);
		}

		return "redirect:/employee/list.do";
	}
	@RequiredPermission("員工刪除")
	@RequestMapping("/delete")
	public String saveDelete(Long id){
		if(id != null){
			employeeService.deleteByPrimaryKey(id);
		}

		return "redirect:/employee/list.do";
	}

	@RequestMapping("/batchDelete")
	public Object batchDelete(Long[] ids){
		JSONResult result = new JSONResult();
		try {
			if(ids != null){
                employeeService.batchDelete(ids);
            }
		} catch (Exception e) {
			e.printStackTrace();
			result.mark(e.getMessage());
		}

		return result;
	}
}*/